import * as THREE from 'three'
export default class Base {
    constructor(){
        // 场景
        this.scene = new THREE.Scene();
        // 摄像机
        this.camera = new THREE.PerspectiveCamera(
            75,
            window.innerWidth / window.innerHeight,
            0.001,
            10000
        );
        this.camera.position.z = 5;
        this.camera.updateProjectionMatrix();
        // 渲染器
        this.renderer = new THREE.WebGLRenderer();
        // 大小
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        // 像素
        this.renderer.setPixelRatio(window.devicePixelRatio)

    }
    update(){
        this.renderer.render(this.scene, this.camera)
    }
    // 自适应
    resize(){
        //监听画面变化，更新渲染画面
        window.addEventListener('resize', () => {
            //更新摄像头
            this.camera.aspect = window.innerWidth / window.innerHeight;
            //更新摄像机的投影矩阵，三维通过矩阵算法映射到屏幕的二维画面
            this.camera.updateProjectionMatrix();
            //更新渲染器
            this.renderer.setSize(window.innerWidth, window.innerHeight);
            //更新渲染器的像素比
            this.renderer.setPixelRatio(window.devicePixelRatio);
            this.renderer.render(this.scene, this.camera);
        })
    }
    // 灯光 环境光
    addAmbientLight(intensity=1, color="0xffffff"){
        let light = new THREE.AmbientLight(color, intensity);
        this.scene.add(light);
        return light
    }
    // 灯光 方向光
    addDirLight(intensity=1, color="0xffffff"){
        let light = new THREE.DirectionalLight(color, intensity);
        this.scene.add(light);
        return light
    }
}